import { _decorator, Component, Node, TiledLayer, TiledMap, TiledObjectGroup } from 'cc';
import { AStar } from '../scripts/astar/AStar';
const { ccclass, property } = _decorator;

@ccclass('Tiledtest')
export class Tiledtest extends Component {
    tiledMap: TiledMap = null;

    @property(TiledLayer)
    userLayer: TiledLayer = null;

    @property(TiledObjectGroup)
    objLayer: TiledObjectGroup = null;

    @property(Node)
    userNode: Node = null;
    @property(Node)
    user2Node: Node = null;

    start() {
        this.tiledMap = this.node.getComponent(TiledMap);
        console.log("tiledMap =>", this.tiledMap);
        console.log("objGroup =>", this.objLayer);
        console.log("getObjects =>", this.objLayer.getObjects());
        
        console.log(this.userLayer);

        let layer = this.tiledMap.getLayer('layer_0');
        console.log("layer 0 =>", layer);
        console.log("layer getTileSets =>", layer.tiles);
        let gid = layer.getTileGIDAt(0, 0);
        console.log("layer getTileGIDAt =>", gid);
        
        
        this.userLayer.addUserNode(this.userNode);
        this.scheduleOnce(() => {
            this.userLayer.addUserNode(this.user2Node);
            layer.setTileGIDAt(5, 1, 0);
        }, 1);


        let aStar = new AStar(10, 10);

        for (let i = 0; i < layer.tiles.length; i++) {
            let x = i % 10;
            let y = Math.floor(i / 10);
            const idx = layer.tiles[i];
            
            if (idx == 0) {
                console.log("i =>", i, "x =>", x, "y =>", y , "idx =>", idx);
                aStar.addObstacle(x, y);
            }
        }

        // 同步模式
        const path = aStar.findPath(0, 0, 0, 9, AStar.Mode.Hexagon_Six);
        console.log(path);
    }

    update(deltaTime: number) {
        
    }
}


